package com.unisoc.keyguard.util;

import com.android.systemui.R;
import com.unisoc.sdk.common.telephony.UniTelephonyManager;

import android.content.Context;
import android.content.res.Resources;
import android.os.SystemProperties;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.lang.Integer;

public class SimLockUtil {
    public static final String TAG = "SimLockUtil";

    public static SimLockUtil mInstance = null;
    public static boolean mSimLockCanceled;

    public static SimLockUtil getInstance(Context context) {
        if(mInstance == null){
            return new SimLockUtil(context);
        }
        return mInstance;
    }

    public SimLockUtil() {
    }

    public SimLockUtil(Context context) {
    }

    public static int getStateByLockStatus(int isNetworkLock, int isNetworkSubsetLock, int isServiceProviderLock, int isCorporateLock, int isSimLock) {
        int state = UniTelephonyManager.SIM_STATE_NETWORK_LOCKED;
        if (isNetworkLock > 0) {
            state = UniTelephonyManager.SIM_STATE_NETWORK_LOCKED;
        }
        if (isNetworkSubsetLock > 0) {
            state = UniTelephonyManager.SIM_STATE_NETWORKSUBSET_LOCKED;
        }
        if (isServiceProviderLock > 0) {
            state = UniTelephonyManager.SIM_STATE_SERVICEPROVIDER_LOCKED;
        }
        if (isCorporateLock > 0) {
            state = UniTelephonyManager.SIM_STATE_CORPORATE_LOCKED;
        }
        if (isSimLock > 0) {
            state = UniTelephonyManager.SIM_STATE_SIM_LOCKED;
        }
        return state;
    }

    public static int getUnlockTypeByState(int state) {
        switch(state){
        case UniTelephonyManager.SIM_STATE_NETWORK_LOCKED:
            return UniTelephonyManager.UNLOCK_NETWORK;
        case UniTelephonyManager.SIM_STATE_NETWORKSUBSET_LOCKED:
            return UniTelephonyManager.UNLOCK_NETWORK_SUBSET;
        case UniTelephonyManager.SIM_STATE_SERVICEPROVIDER_LOCKED:
            return UniTelephonyManager.UNLOCK_SERVICE_PORIVDER;
        case UniTelephonyManager.SIM_STATE_CORPORATE_LOCKED:
            return UniTelephonyManager.UNLOCK_CORPORATE;
        case UniTelephonyManager.SIM_STATE_SIM_LOCKED:
            return UniTelephonyManager.UNLOCK_SIM;
        case UniTelephonyManager.SIM_STATE_NETWORK_LOCKED_PUK:
            return UniTelephonyManager.UNLOCK_NETWORK_PUK;
        case UniTelephonyManager.SIM_STATE_NETWORK_SUBSET_LOCKED_PUK:
            return UniTelephonyManager.UNLOCK_NETWORK_SUBSET_PUK;
        case UniTelephonyManager.SIM_STATE_SERVICE_PROVIDER_LOCKED_PUK:
            return UniTelephonyManager.UNLOCK_SERVICE_PORIVDER_PUK;
        case UniTelephonyManager.SIM_STATE_CORPORATE_LOCKED_PUK:
            return UniTelephonyManager.UNLOCK_CORPORATE_PUK;
        case UniTelephonyManager.SIM_STATE_SIM_LOCKED_PUK:
            return UniTelephonyManager.UNLOCK_SIM_PUK;
        default:
            return UniTelephonyManager.SIM_STATE_NETWORK_LOCKED;
        }
    }

    public static int getTitleByState(int state) {
        switch(state){
        case UniTelephonyManager.SIM_STATE_NETWORK_LOCKED:
            return R.string.keyguard_simlock_network_locked_message;
        case UniTelephonyManager.SIM_STATE_NETWORKSUBSET_LOCKED:
            return R.string.keyguard_simlock_networksubset_locked_message;
        case UniTelephonyManager.SIM_STATE_SERVICEPROVIDER_LOCKED:
            return R.string.keyguard_simlock_serviceprovider_locked_message;
        case UniTelephonyManager.SIM_STATE_CORPORATE_LOCKED:
            return R.string.keyguard_simlock_corprorate_locked_message;
        case UniTelephonyManager.SIM_STATE_SIM_LOCKED:
            return R.string.keyguard_simlock_sim_locked_message;
        case UniTelephonyManager.SIM_STATE_NETWORK_LOCKED_PUK:
            return R.string.keyguard_simlock_network_puk_locked__message;
        case UniTelephonyManager.SIM_STATE_NETWORK_SUBSET_LOCKED_PUK:
            return R.string.keyguard_simlock_networksubset_puk_locked_message;
        case UniTelephonyManager.SIM_STATE_SERVICE_PROVIDER_LOCKED_PUK:
            return R.string.keyguard_simlock_serviceprovider_puk_locked_message;
        case UniTelephonyManager.SIM_STATE_CORPORATE_LOCKED_PUK:
            return R.string.keyguard_simlock_corprorate_puk_locked_message;
        case UniTelephonyManager.SIM_STATE_SIM_LOCKED_PUK:
            return R.string.keyguard_simlock_sim_puk_locked_message;
        default:
            return R.string.keyguard_simlock_default_message;
        }
    }

    public static String getFacility(int unlockType) {
        String facility = "";
        switch (unlockType) {
        case UniTelephonyManager.UNLOCK_SIM:
            facility = UniTelephonyManager.CB_FACILITY_BA_PS;
            break;
        case UniTelephonyManager.UNLOCK_NETWORK:
            facility = UniTelephonyManager.CB_FACILITY_BA_PN;
            break;
        case UniTelephonyManager.UNLOCK_NETWORK_SUBSET:
            facility = UniTelephonyManager.CB_FACILITY_BA_PU;
            break;
        case UniTelephonyManager.UNLOCK_SERVICE_PORIVDER:
            facility = UniTelephonyManager.CB_FACILITY_BA_PP;
            break;
        case UniTelephonyManager.UNLOCK_CORPORATE:
            facility = UniTelephonyManager.CB_FACILITY_BA_PC;
            break;
        case UniTelephonyManager.UNLOCK_NETWORK_PUK:
            facility = UniTelephonyManager.CB_FACILITY_BA_PN_PUK;
            break;
        case UniTelephonyManager.UNLOCK_NETWORK_SUBSET_PUK:
            facility = UniTelephonyManager.CB_FACILITY_BA_PU_PUK;
            break;
        case UniTelephonyManager.UNLOCK_SERVICE_PORIVDER_PUK:
            facility = UniTelephonyManager.CB_FACILITY_BA_PP_PUK;
            break;
        case UniTelephonyManager.UNLOCK_CORPORATE_PUK:
            facility = UniTelephonyManager.CB_FACILITY_BA_PC_PUK;
            break;
        case UniTelephonyManager.UNLOCK_SIM_PUK:
            facility = UniTelephonyManager.CB_FACILITY_BA_PS_PUK;
            break;
        default :
            facility = UniTelephonyManager.CB_FACILITY_BA_PN;
            break;
        }
        return facility;
    }

    public static boolean isSimLockState(int state) {
        int[] stateArray = {
                UniTelephonyManager.SIM_STATE_NETWORK_LOCKED,
                UniTelephonyManager.SIM_STATE_NETWORKSUBSET_LOCKED,
                UniTelephonyManager.SIM_STATE_SERVICEPROVIDER_LOCKED,
                UniTelephonyManager.SIM_STATE_CORPORATE_LOCKED,
                UniTelephonyManager.SIM_STATE_SIM_LOCKED,
                UniTelephonyManager.SIM_STATE_NETWORK_LOCKED_PUK,
                UniTelephonyManager.SIM_STATE_NETWORK_SUBSET_LOCKED_PUK,
                UniTelephonyManager.SIM_STATE_SERVICE_PROVIDER_LOCKED_PUK,
                UniTelephonyManager.SIM_STATE_CORPORATE_LOCKED_PUK,
                UniTelephonyManager.SIM_STATE_SIM_LOCKED_PUK,
                UniTelephonyManager.SIM_STATE_SIM_LOCKED_PERMANENTLY
        };
        List<Integer> stateList = Arrays.stream(stateArray).boxed().collect(Collectors.toList());
        return stateList.contains(Integer.valueOf(state));
    }

    /** Return a boolean flag which means whether showing SimLock secure view
     *  automatically, when current state is SimLock */
    public static boolean isAutoShow() {
        if (Resources.getSystem().getBoolean(com.android.internal.R.bool.simlock_hide_unlock_view)) {
            return Resources.getSystem().getBoolean(com.android.internal.R.bool.simlock_show_operator_view);
        }
        return !Resources.getSystem().getBoolean(com.android.internal.R.bool.config_subsidyLock)
                && SystemProperties.getBoolean("ro.simlock.unlock.autoshow", true)
                && !SystemProperties.getBoolean("ro.simlock.onekey.lock", false)
                && !SystemProperties.getBoolean("ro.simlock.unlock.bynv", false);
    }

    /** Return a boolean flag which means SimLock secure view will not show
     *  automatically until user enter secrect code by Dialer */
    public static boolean isByNv() {
        return SystemProperties.getBoolean("ro.simlock.unlock.bynv", false);
    }

    /** Rerurn a boolean flag which means user can lock and unclock SimLock by secrect code */
    public static boolean isOnekeyLock() {
        return SystemProperties.getBoolean("ro.simlock.onekey.lock", false);
    }

    /**
     * For operator SimLock:
     * Always display simlock security screen(KeyguardOperatorSimLockView) when all sims absent @{
     */
    public boolean showSimLockWhenSimsAbsent(Context context) {
        if (Resources.getSystem().getBoolean(com.android.internal.R.bool.simlock_hide_unlock_view)
                && Resources.getSystem().getBoolean(com.android.internal.R.bool.simlock_show_operator_view)
                && Resources.getSystem().getBoolean(com.android.internal.R.bool.simlock_show_operator_view_simsabsent)) {
            boolean allSimsAbsent = true;
            TelephonyManager telephonyManager = TelephonyManager.from(context);
            for (int i = 0; i < telephonyManager.getPhoneCount(); i++) {
                if (telephonyManager.hasIccCard(i)) {
                    allSimsAbsent = false;
                }
            }
            return allSimsAbsent;
        }
        return false;
    }
}
